package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.User;
import com.qk.service.UserService;
import com.qk.dto.UserDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;
    //查询用户列表
    @GetMapping
    public Result list(UserDto userDto){
        log.info("查询用户列表");
        //调用Service 方法
        PageResult<User> pageResult = userService.list(userDto);
        return Result.success(pageResult);
    }
    //删除用户
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable("ids") List<Integer> ids){
        log.info("批量删除用户{}",ids);
        //调用Service 方法
        userService.deleteByIds(ids);
        return Result.success();
    }
    //添加用户
    @PostMapping
    public Result add(@RequestBody User user){
        log.info("添加用户");
        //调用Service 方法
        userService.add(user);
        return Result.success();
    }
    //根据id查询用户
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("查询用户");
        //调用Service 方法
        User user = userService.getById(id);
        return Result.success(user);
    }
    //更新用户
    @PutMapping
    public Result update(@RequestBody User user){
        log.info("更新用户");
        //调用Service 方法
        userService.update(user);
        return Result.success();
    }

    //查询所有用户
    @GetMapping("/list")
    public Result listAll(){
        log.info("查询所有用户");
        //调用Service 方法
        List<User> list = userService.listAll();
        return Result.success(list);
    }

    //根据角色查询用户
    @GetMapping("/role/{roleLabel}")
    public Result listByRole(@PathVariable String roleLabel){
        log.info("根据角色查询用户");
        //调用Service 方法
        List<User> list = userService.listByRole(roleLabel);
        return Result.success(list);
    }

    //根据部门查询用户
    @GetMapping("/dept/{deptId}")
    public  Result listByDept(@PathVariable Integer deptId){
        log.info("根据部门查询用户");
        //调用Service 方法
        List<User> list = userService.listByDept(deptId);
        return Result.success(list);
    }
}
